<?php
namespace Wechat\Controller;

class GoodController extends BaseController{

    /*商品信息首页*/
    public function index()
    {
        $count      = M('goods')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('goods')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $k=>$v) {
            $list[$k]['goods_type'] = M('goodstype')->where(['id' => $v['goods_type']])->field('goods_name')->find();
            if($list[$k]['goods_shangjia']==1){
                $list[$k]['goods_shangjia']='已上架';
            }else{
                $list[$k]['goods_shangjia']='未上架';
            }
            if($list[$k]['goods_top']==1){
                $list[$k]['goods_top']='已顶置';
            }else{
                $list[$k]['goods_top']='未顶置';
            }
            /*商品品类信息，0为热销，1为新品，2为精品*/
            if($list[$k]['goods_hot']==0){
                $list[$k]['goods_hot']='热销';
            }elseif ($list[$k]['goods_hot']==1){
                $list[$k]['goods_hot']='新品';
            }else{
                $list[$k]['goods_hot']='精品';
            }
        }
        /*
        echo '<pre/>';
        var_dump($list);
        die();
        */
        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }

    /*商品添加*/
    public function add()
    {
        $post = i('post.');
        $this->assign('type',M('goodstype')->select());
        if(IS_POST) {
            /*
            echo '<pre/>';
            var_dump($_POST);
            die();
            */
            /*
            $dd = implode('|',$post['photos_url']);
            $pp = explode('|',$dd);
            echo '<pre>';
            var_dump($pp);
            die();
            */
            if (trim(empty($post['good_xiangxi']))) {
                $this->error('详细描述不能为空');
            }
            if (trim(empty($post['good_name']))) {
                $this->error('商品名称不能为空');
            }
            if (trim(empty($post['good_thumb']))) {
                $this->error('请上传商品封面图');
            }
            if (!isset($post['photos_url'])) {
                $this->error('请上传商品展示图');
            }
            if (!isset($post['good_chengjiao'])) {
                $this->error('请输入商品成交量');
            }
            if (!isset($post['good_kucun'])) {
                $this->error('请输入商品库存');
            }
            if (!isset($post['good_xiaoshou'])) {
                $this->error('请输入商品销售量');
            }
            /*
            echo '<pre/>';
            var_dump($post);
            */
            $data = [
                'goods_name' => $post['good_name'],/*商品名称*/
                'goods_num' => $post['good_num'],/*商品货号*/
                'goods_chengjiao' => round($post['good_chengjiao']),/*商品成交量*/
                'goods_kucun' => round($post['good_kucun']),/*商品库存*/
                'goods_xiaoshou' => round($post['good_xiaoshou']),/*商品销售价*/
                'goods_shichang' => round($post['good_shichang']),/*商品市场价*/
                'goods_vip' => round($post['good_huiyuan']),/*商品会员价*/
                'goods_jifen' => round($post['good_zengsong']),/*商品赠送积分*/
                'goods_key' => $post['good_key'],/*商品关键词*/
                'goods_beizhu' => $post['good_beizhu'],/*商品商家备注*/
                'goods_show' => implode('|', $post['photos_url']),/*商品展示图*/
                'goods_des' => $post['good_miaoshu'],/*商品描述*/
                'goods_xiangxi' => $post['good_xiangxi'],/*商品详细描述*/
                'goods_thumb' => $post['good_thumb'],/*商品封面图*/
                'goods_time' => $post['good_time'],/*商品发布时间*/
                'goods_shangjia' => $post['good_shangjia'],/*商品上架信息1为上架，0为未上架*/
                'goods_top' => $post['good_top'],/*商品顶置信息，1为顶置，0为未顶置，默认为0*/
                'goods_hot' => $post['good_hot'],/*商品品类信息，0为热销，1为新品，2为精品*/
                'goods_type' => $post['type'],/*商品分类信息*/
                'goods_maker'=>$_SESSION['name'],/*商品发布者*/
                'goods_youfei'=>$post['good_youfei']/*邮费*/
            ];
            if(M('goods')->add($data)){
                $this->success('添加成功哦，^_^~~!');
            }else{
                $this->error('添加失败，未知错误。T_T~~!');
            }

        }else{
            $this->display();
        }

    }
    /*商品删除*/
    public function del()
    {
        $id = i('get.id');
        if(M('goods')->where(['id'=>$id])->delete()){
            $this->success('成功~');
        }else{
            $this->error('失败了~未知错误。');
        }
			
		
    }
    /*商品编辑*/
    public function edit()
    {
        $id = i('get.id');
        $post = i('post.');
        $this->assign('type',M('goodstype')->select());
        $this->assign('voi',M('goods')->where(['id'=>$id])->find());

        if(IS_POST) {
            if (trim(empty($post['good_name']))) {
                $this->error('商品名称不能为空');
            }
            if (trim(empty($post['good_thumb']))) {
                $this->error('请上传商品封面图');
            }
            if (!isset($post['photos_url'])) {
                $this->error('请上传商品展示图');
            }
            if (!isset($post['good_chengjiao'])) {
                $this->error('请输入商品成交量');
            }
            if (!isset($post['good_kucun'])) {
                $this->error('请输入商品库存');
            }
            if (!isset($post['good_xiaoshou'])) {
                $this->error('请输入商品销售量');
            }
            $data = [
                'goods_name' => $post['good_name'],/*商品名称*/
                'goods_num' => $post['good_num'],/*商品货号*/
                'goods_chengjiao' => round($post['good_chengjiao']),/*商品成交量*/
                'goods_kucun' => round($post['good_kucun']),/*商品库存*/
                'goods_xiaoshou' => round($post['good_xiaoshou']),/*商品销售价*/
                'goods_shichang' => round($post['good_shichang']),/*商品市场价*/
                'goods_vip' => round($post['good_huiyuan']),/*商品会员价*/
                'goods_jifen' => round($post['good_zengsong']),/*商品赠送积分*/
                'goods_key' => $post['good_key'],/*商品关键词*/
                'goods_beizhu' => $post['good_beizhu'],/*商品商家备注*/
                'goods_show' => implode('|', $post['photos_url']),/*商品展示图*/
                'goods_des' => $post['good_miaoshu'],/*商品描述*/
                'goods_thumb' => $post['good_thumb'],/*商品封面图*/
                'goods_time' => $post['good_time'],/*商品发布时间*/
                'goods_shangjia' => $post['good_shangjia'],/*商品上架信息1为上架，0为未上架*/
                'goods_top' => $post['good_top'],/*商品顶置信息，1为顶置，0为未顶置，默认为0*/
                'goods_hot' => $post['good_hot'],/*商品品类信息，0为热销，1为新品，2为精品*/
                'goods_type' => $post['type'],/*商品分类信息*/
                'goods_maker'=>$_SESSION['name'],/*商品发布者*/
                'goods_youfei'=>$post['good_youfei']/*邮费*/
            ];
            if(M('goods')->where(['id'=>$id])->save($data)){
                $this->success('修改成功哦，^_^~~!');
            }else{
                $this->error('修改失败，未知错误。T_T~~!');
            }

        }else{
            $this->display();
        }
    }
    /*搜索页*/
    public function ser()
    {
        $ser = i('post.ser');
        $where= array('like','%'.$ser.'%');
        if(IS_POST){
            if(trim(empty($ser))){
                $this->error('查询内容不能为空');
            }
            $count      = M('goods')->where(['goods_name'=>$where])->count();// 查询满足要求的总记录数
            if($count==0){
                $this->error('没有查到您输入的内容，抱歉啦。');
            }
            $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
            $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
            $show       = $Page->show();// 分页显示输出

            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $list = M('goods')->where(['goods_name'=>$where])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            foreach ($list as $k=>$v) {
                $list[$k]['goods_type'] = M('goodstype')->where(['id' => $v['goods_type']])->field('goods_name')->find();
                if($list[$k]['goods_shangjia']==1){
                    $list[$k]['goods_shangjia']='已上架';
                }else{
                    $list[$k]['goods_shangjia']='未上架';
                }
                if($list[$k]['goods_top']==1){
                    $list[$k]['goods_top']='已顶置';
                }else{
                    $list[$k]['goods_top']='未顶置';
                }
                /*商品品类信息，0为热销，1为新品，2为精品*/
                if($list[$k]['goods_hot']==0){
                    $list[$k]['goods_hot']='热销';
                }elseif ($list[$k]['goods_hot']==1){
                    $list[$k]['goods_hot']='新品';
                }else{
                    $list[$k]['goods_hot']='精品';
                }
            }
            /*
            echo '<pre/>';
            var_dump($list);
            die();
            */
            $this->assign(['page'=>$show,'data'=>$list]);
            $this->display();
        }else{
            $this->display();
        }
    }

    /*批量删除*/
    public function getpost()
    {
        $post = i('post.');
        /*
        echo '<pre/>';
        var_dump($post);
        die();
        */
        if(IS_POST) {
            foreach ($post['ids'] as $k) {
                M('goods')->where(['id' => $k])->delete();
            }
            $this->redirect('Good/index');
        }
    }
}